package com.hanggold.springboot.lesson4.condition;

import com.hanggold.springboot.lesson4.annotation.ConditionalOnSystemPropertyCondition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;


public class OnSystemPropertiesCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {


        MultiValueMap<String, Object> map = metadata.getAllAnnotationAttributes(ConditionalOnSystemPropertyCondition.class.getName());
//        Map<String, Object> map = metadata.getAnnotationAttributes(OnSystemPropertiesCondition.class.getName());




        String propertyName = (String) map.getFirst("name");

        System.out.println("language: " + propertyName);

        String systemLanguage = System.getProperty(propertyName, "Chinese");

        String conditionLanguage = (String) map.getFirst("value");

        if (systemLanguage.equalsIgnoreCase(conditionLanguage)) {
            return true;
        }
        return false;
    }
}
